package com.example.zy;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

public class Main2Activity extends AppCompatActivity  {

    private ArrayList<MenuItemBean> mlistMenuItemBean ;
    private android.support.v4.widget.SlidingPaneLayout mSlidingPaneLayout;

    private MenuItemBean createMenuItemBean(int menuItemImage, String menuItemTitle) {
        MenuItemBean menuItemBean = new MenuItemBean();
        menuItemBean.menuItemImage = menuItemImage;
        menuItemBean.menuItemTitle = menuItemTitle;
        return menuItemBean;
    }
    protected void initData() {
        mlistMenuItemBean = new ArrayList<MenuItemBean>();
        mlistMenuItemBean.add(createMenuItemBean(R.mipmap.tab_find_sel, "ActionBar"));
        mlistMenuItemBean.add(createMenuItemBean(R.mipmap.tab_news_sel, "ActionBarTab"));
        mlistMenuItemBean.add(createMenuItemBean(R.mipmap.tab_read_sel, "选项菜单"));
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initData();

        SlidingpanemenuListviewItemAdapter adapter = new SlidingpanemenuListviewItemAdapter(this);
        ListView listView = findViewById(R.id.listView1);
        listView.setAdapter(adapter);

        mSlidingPaneLayout = findViewById(R.id.slidingPaneLayout);
        mSlidingPaneLayout.openPane();

        ImageView imageView4HD = findViewById(R.id.iV_Hidden_Display_SlidingPane);
        imageView4HD.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mSlidingPaneLayout.isOpen()){
                    mSlidingPaneLayout.closePane();
                }else {
                    mSlidingPaneLayout.openPane();
                }
            }
        });

    }

    private class MenuItemBean {
        int menuItemImage;
        String menuItemTitle;
    }

    private class SlidingpanemenuListviewItemAdapter extends BaseAdapter {

        private Context context;
        private LayoutInflater layoutInflater;

        public SlidingpanemenuListviewItemAdapter(Context context) {
            this.context = context;
            this.layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return mlistMenuItemBean.size();
        }

        @Override
        public MenuItemBean getItem(int position) {
            return mlistMenuItemBean.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.slidingpanemenu_listview_item, null);
                convertView.setTag(new ViewHolder(convertView));
            }
            initializeViews((MenuItemBean)getItem(position), (ViewHolder) convertView.getTag());
            return convertView;
        }

        private void initializeViews(MenuItemBean object, ViewHolder holder) {
            holder.menuitemIcon.setImageResource(object.menuItemImage);
            holder.menuitemTitle.setText(object.menuItemTitle);
        }

        protected class ViewHolder {
            private ImageView menuitemIcon;
            private TextView menuitemTitle;

            public ViewHolder(View view) {
                menuitemIcon = (ImageView) view.findViewById(R.id.menuitem_icon);
                menuitemTitle = (TextView) view.findViewById(R.id.menuitem_title);
            }
        }
    }
}
